﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using TamoCRM.Core;
using TamoCRM.Core.Commons.Extensions;
using TamoCRM.Domain.Activity;
using TamoCRM.Domain.Branches;
using TamoCRM.Domain.Channels;
using TamoCRM.Domain.Collectors;
using TamoCRM.Domain.Contacts;
using TamoCRM.Domain.EducationLevels;
using TamoCRM.Domain.Gender;
using TamoCRM.Domain.Groups;
using TamoCRM.Domain.Levels;
using TamoCRM.Domain.Locations;
using TamoCRM.Domain.Majors;
using TamoCRM.Domain.Schools;
using TamoCRM.Domain.SourceTypeMap;
using TamoCRM.Domain.SourceTypes;
using TamoCRM.Domain.Status;
using TamoCRM.Domain.StatusCare;
using TamoCRM.Domain.StatusMap;
using TamoCRM.Domain.UserRole;
using TamoCRM.Services.Activity;
using TamoCRM.Services.Branches;
using TamoCRM.Services.Channels;
using TamoCRM.Services.Collectors;
using TamoCRM.Services.EducationLevels;
using TamoCRM.Services.Groups;
using TamoCRM.Services.Levels;
using TamoCRM.Services.Locations;
using TamoCRM.Services.Majors;
using TamoCRM.Services.Schools;
using TamoCRM.Services.SourceTypeMap;
using TamoCRM.Services.SourceTypes;
using TamoCRM.Services.Status;
using TamoCRM.Services.StatusCare;
using TamoCRM.Services.StatusMap;
using TamoCRM.Services.UserRole;
using TamoCRM.Web.Framework.ActionFilters;
using TamoCRM.Web.Framework.Users;

namespace TamoCRM.Web.Framework.Controllers
{
    [AdminActionFilter]
    public class AdminController : BaseController
    {
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);

            var userCurent = UserContext.GetCurrentUser();
            if (userCurent != null)
            {
                ViewBag.GroupId = userCurent.GroupId;
                ViewBag.GroupType = userCurent.GroupType;
            }

            // GroupType
            var groupTypes = new Dictionary<int, string>();
            foreach (var item in Enum.GetValues(typeof(GroupType)))
                groupTypes.Add((int)item, ObjectExtensions.GetEnumDescription((GroupType)item));
            ViewBag.GroupTypes = groupTypes.Select(c => new { Id = c.Key, Name = c.Value });

            filterContext.Controller.ViewBag.BranchId = UserContext.GetDefaultBranch();
            filterContext.Controller.ViewBag.UserId = UserContext.GetCurrentUser().UserID;

            // Group
            if (StoreData.ListGroup.IsNullOrEmpty()) StoreData.ListGroup = GroupRepository.GetAll() ?? new List<GroupInfo>();
            StoreData.ListGroup.RemoveAll(c => c.GroupId == 0);
            filterContext.Controller.ViewBag.Groups = StoreData.ListGroup;

            // User
            if (StoreData.ListUser.IsNullOrEmpty()) StoreData.ListUser = UserRepository.GetAll() ?? new List<UserInfo>();
            StoreData.ListUser.RemoveAll(c => c.UserID == 0);
            filterContext.Controller.ViewBag.Users = StoreData.ListUser;

            // Major
            if (StoreData.ListMajor.IsNullOrEmpty()) StoreData.ListMajor = MajorRepository.GetAll() ?? new List<MajorInfo>();
            StoreData.ListMajor.RemoveAll(c => c.MajorId == 0);
            filterContext.Controller.ViewBag.Majors = StoreData.ListMajor.OrderBy(c => c.Name.Length).ToList();

            // Level
            if (StoreData.ListLevel.IsNullOrEmpty()) StoreData.ListLevel = LevelRepository.GetAll() ?? new List<LevelInfo>();
            StoreData.ListLevel.RemoveAll(c => c.LevelId == 0);
            filterContext.Controller.ViewBag.Levels = StoreData.ListLevel;

            // Status
            if (StoreData.ListStatus.IsNullOrEmpty()) StoreData.ListStatus = StatusRepository.GetAll() ?? new List<StatusInfo>();
            StoreData.ListStatus.RemoveAll(c => c.StatusId == 0);
            filterContext.Controller.ViewBag.Status = StoreData.ListStatus;

            // School
            if (StoreData.ListSchool.IsNullOrEmpty()) StoreData.ListSchool = SchoolRepository.GetAll() ?? new List<SchoolInfo>();
            StoreData.ListSchool.RemoveAll(c => c.SchoolId == 0);
            filterContext.Controller.ViewBag.Schools = StoreData.ListSchool.OrderBy(c => c.Name.Length).ToList();

            // Branch
            if (StoreData.ListBranch.IsNullOrEmpty()) StoreData.ListBranch = BranchRepository.GetAll() ?? new List<BranchInfo>();
            StoreData.ListBranch.RemoveAll(c => c.BranchId == 0);
            filterContext.Controller.ViewBag.Branches = StoreData.ListBranch;

            // Channel
            if (StoreData.ListChannel.IsNullOrEmpty()) StoreData.ListChannel = ChannelRepository.GetAll() ?? new List<ChannelInfo>();
            StoreData.ListChannel.RemoveAll(c => c.ChannelId == 0);
            filterContext.Controller.ViewBag.Channels = StoreData.ListChannel;

            // Location
            if (StoreData.ListLocation.IsNullOrEmpty()) StoreData.ListLocation = LocationRepository.GetAll() ?? new List<LocationInfo>();
            StoreData.ListLocation.RemoveAll(c => c.LocationId == 0);
            filterContext.Controller.ViewBag.Locations = StoreData.ListLocation;

            // Collector
            if (StoreData.ListCollector.IsNullOrEmpty()) StoreData.ListCollector = CollectorRepository.GetAll() ?? new List<CollectorInfo>();
            StoreData.ListCollector.RemoveAll(c => c.CollectorId == 0);
            filterContext.Controller.ViewBag.Collectors = StoreData.ListCollector;

            // StatusMap
            if (StoreData.ListStatusMap.IsNullOrEmpty()) StoreData.ListStatusMap = StatusMapRepository.GetAll() ?? new List<StatusMapInfo>();
            StoreData.ListStatusMap.RemoveAll(c => c.Id == 0);
            filterContext.Controller.ViewBag.StatusMaps = StoreData.ListStatusMap;

            // StatusCare
            if (StoreData.ListStatusCare.IsNullOrEmpty()) StoreData.ListStatusCare = StatusCareRepository.GetAll() ?? new List<StatusCareInfo>();
            StoreData.ListStatusCare.RemoveAll(c => c.Id == 0);
            filterContext.Controller.ViewBag.StatusCares = StoreData.ListStatusCare;

            // SourceType
            if (StoreData.ListSourceType.IsNullOrEmpty()) StoreData.ListSourceType = SourceTypeRepository.GetAll() ?? new List<SourceTypeInfo>();
            StoreData.ListSourceType.RemoveAll(c => c.SourceTypeId == 0);
            filterContext.Controller.ViewBag.SourceTypes = StoreData.ListSourceType;

            // SourceTypeMap
            if (StoreData.ListSourceTypeMap.IsNullOrEmpty()) StoreData.ListSourceTypeMap = SourceTypeMapRepository.GetAll() ?? new List<SourceTypeMapInfo>();
            StoreData.ListSourceTypeMap.RemoveAll(c => c.Id == 0);
            filterContext.Controller.ViewBag.SourceTypeMaps = StoreData.ListSourceTypeMap;

            // EducationLevel
            if (StoreData.ListEducationLevel.IsNullOrEmpty()) StoreData.ListEducationLevel = EducationLevelRepository.GetAll() ?? new List<EducationLevelInfo>();
            StoreData.ListEducationLevel.RemoveAll(c => c.EducationLevelId == 0);
            filterContext.Controller.ViewBag.EducationLevels = StoreData.ListEducationLevel;

            // Gender
            filterContext.Controller.ViewBag.Genders = new List<GenderInfo>
                            {
                                new GenderInfo {Code = 0, Name = "Nam"},
                                new GenderInfo {Code = 1, Name = "Nữ"}
                            };

            // Appointment
            filterContext.Controller.ViewBag.Appointments = new List<AppointmentTypes>
                                       {
                                           new AppointmentTypes { Code = (int) AppointmentType.Other, Name = "Khác" },
                                           new AppointmentTypes { Code = (int) AppointmentType.Direct, Name = "Trực tiếp" },
                                           new AppointmentTypes { Code = (int) AppointmentType.InDirect, Name = "Chuyển khoản" },
                                       };

            // SubmitedProfileType
            filterContext.Controller.ViewBag.SubmitedProfileTypes = new List<SubmitedProfileTypes>
                                               {
                                                   new SubmitedProfileTypes { Code = (int) SubmitedProfileType.Applied, Name = "Đã nộp" },
                                                   new SubmitedProfileTypes { Code = (int) SubmitedProfileType.NotApplied, Name = "Chưa nộp" },
                                                   new SubmitedProfileTypes { Code = (int) SubmitedProfileType.AppliedNotEnough, Name = "Đã nộp nhưng chưa đủ" },
                                               };
            try
            {
                //Apply logging
                var objChanges = HttpContext.Items[ContactInfo.CONTACT_CHANGE] as List<ActivityObjectChangeInfo>;
                if (objChanges != null)
                {
                    var activityInfo = new ActivityLogInfo
                                           {
                                               FunctionId = 0,
                                               CreatedBy = UserContext.GetCurrentUser().UserID
                                           };
                    activityInfo.Id = ActivityLogRepository.Create(activityInfo);
                    foreach (var objChange in objChanges)
                    {
                        objChange.ActivityId = activityInfo.Id;
                        ActivityObjectChangeRepository.Create(objChange);
                    }
                }
                HttpContext.Items[ContactInfo.CONTACT_CHANGE] = null;
            }
            catch
            {
                //Dont throw exception if loging failed
            }
        }
    }
}
